<html>
<head><meta charset="utf-8"><title>get the (maybe) def path of an InstanceDef · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/get.20the.20(maybe).20def.20path.20of.20an.20InstanceDef.html">get the (maybe) def path of an InstanceDef</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="214460602"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/get%20the%20%28maybe%29%20def%20path%20of%20an%20InstanceDef/near/214460602" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> sfzhu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/get.20the.20(maybe).20def.20path.20of.20an.20InstanceDef.html#214460602">(Oct 25 2020 at 01:25)</a>:</h4>
<p>when using <code>debug!("{:?}")</code> I can print the <code>InstanceDef</code> type in <code>rustc_middle/src/ty/instance.rs</code> in a form like this: <code>Item(WithOptConstParam { did: DefId(2:22867 ~ core[141f]::ops[0]::arith[0]::{{impl}}[13]::add[0]), const_param_did: None })</code>. How can I get it as a string or a struct? I'd like to know some information of the string. Inside the struct there's only id indices in the HIR.</p>



<a name="214460943"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/get%20the%20%28maybe%29%20def%20path%20of%20an%20InstanceDef/near/214460943" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> sfzhu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/get.20the.20(maybe).20def.20path.20of.20an.20InstanceDef.html#214460943">(Oct 25 2020 at 01:33)</a>:</h4>
<p>This piece of code obviously didn't print the above string:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">default_def_id_debug</span><span class="p">(</span><span class="n">def_id</span>: <span class="nc">DefId</span><span class="p">,</span><span class="w"> </span><span class="n">f</span>: <span class="kp">&amp;</span><span class="nc">mut</span><span class="w"> </span><span class="n">fmt</span>::<span class="n">Formatter</span><span class="o">&lt;'</span><span class="nb">_</span><span class="o">&gt;</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">fmt</span>::<span class="nb">Result</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">f</span><span class="p">.</span><span class="n">debug_struct</span><span class="p">(</span><span class="s">"DefId"</span><span class="p">).</span><span class="n">field</span><span class="p">(</span><span class="s">"krate"</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">def_id</span><span class="p">.</span><span class="n">krate</span><span class="p">).</span><span class="n">field</span><span class="p">(</span><span class="s">"index"</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">def_id</span><span class="p">.</span><span class="n">index</span><span class="p">).</span><span class="n">finish</span><span class="p">()</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">pub</span><span class="w"> </span><span class="k">static</span><span class="w"> </span><span class="n">DEF_ID_DEBUG</span>: <span class="nc">AtomicRef</span><span class="o">&lt;</span><span class="k">fn</span><span class="p">(</span><span class="n">DefId</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="n">fmt</span>::<span class="n">Formatter</span><span class="o">&lt;'</span><span class="nb">_</span><span class="o">&gt;</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">fmt</span>::<span class="nb">Result</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"></span>
<span class="w">    </span><span class="n">AtomicRef</span>::<span class="n">new</span><span class="p">(</span><span class="o">&amp;</span><span class="p">(</span><span class="n">default_def_id_debug</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="k">fn</span><span class="p">(</span><span class="n">_</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="n">fmt</span>::<span class="n">Formatter</span><span class="o">&lt;'</span><span class="nb">_</span><span class="o">&gt;</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">_</span><span class="p">));</span><span class="w"></span>

<span class="k">impl</span><span class="w"> </span><span class="n">fmt</span>::<span class="n">Debug</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">DefId</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">fmt</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">,</span><span class="w"> </span><span class="n">f</span>: <span class="kp">&amp;</span><span class="nc">mut</span><span class="w"> </span><span class="n">fmt</span>::<span class="n">Formatter</span><span class="o">&lt;'</span><span class="nb">_</span><span class="o">&gt;</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">fmt</span>::<span class="nb">Result</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="p">(</span><span class="o">*</span><span class="n">DEF_ID_DEBUG</span><span class="p">)(</span><span class="o">*</span><span class="bp">self</span><span class="p">,</span><span class="w"> </span><span class="n">f</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="214462171"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/get%20the%20%28maybe%29%20def%20path%20of%20an%20InstanceDef/near/214462171" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> sfzhu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/get.20the.20(maybe).20def.20path.20of.20an.20InstanceDef.html#214462171">(Oct 25 2020 at 02:04)</a>:</h4>
<p>I have found it: in <code>rustc_interface/src/callback.rs</code> it is set to another function</p>



<a name="214462207"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/get%20the%20%28maybe%29%20def%20path%20of%20an%20InstanceDef/near/214462207" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oliver <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/get.20the.20(maybe).20def.20path.20of.20an.20InstanceDef.html#214462207">(Oct 25 2020 at 02:04)</a>:</h4>
<p>which other function?</p>



<a name="214462405"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/get%20the%20%28maybe%29%20def%20path%20of%20an%20InstanceDef/near/214462405" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> sfzhu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/get.20the.20(maybe).20def.20path.20of.20an.20InstanceDef.html#214462405">(Oct 25 2020 at 02:10)</a>:</h4>
<p><span class="user-mention" data-user-id="281739">@oliver</span> </p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="sd">/// This is a callback from librustc_hir as it cannot access the implicit state</span>
<span class="sd">/// in librustc_middle otherwise.</span>
<span class="k">fn</span> <span class="nf">def_id_debug</span><span class="p">(</span><span class="n">def_id</span>: <span class="nc">rustc_hir</span>::<span class="n">def_id</span>::<span class="n">DefId</span><span class="p">,</span><span class="w"> </span><span class="n">f</span>: <span class="kp">&amp;</span><span class="nc">mut</span><span class="w"> </span><span class="n">fmt</span>::<span class="n">Formatter</span><span class="o">&lt;'</span><span class="nb">_</span><span class="o">&gt;</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">fmt</span>::<span class="nb">Result</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">write</span><span class="o">!</span><span class="p">(</span><span class="n">f</span><span class="p">,</span><span class="w"> </span><span class="s">"DefId({}:{}"</span><span class="p">,</span><span class="w"> </span><span class="n">def_id</span><span class="p">.</span><span class="n">krate</span><span class="p">,</span><span class="w"> </span><span class="n">def_id</span><span class="p">.</span><span class="n">index</span><span class="p">.</span><span class="n">index</span><span class="p">())</span><span class="o">?</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="n">tls</span>::<span class="n">with_opt</span><span class="p">(</span><span class="o">|</span><span class="n">opt_tcx</span><span class="o">|</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="n">tcx</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">opt_tcx</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="n">write</span><span class="o">!</span><span class="p">(</span><span class="n">f</span><span class="p">,</span><span class="w"> </span><span class="s">" ~ {}"</span><span class="p">,</span><span class="w"> </span><span class="n">tcx</span><span class="p">.</span><span class="n">def_path_debug_str</span><span class="p">(</span><span class="n">def_id</span><span class="p">))</span><span class="o">?</span><span class="p">;</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">        </span><span class="nb">Ok</span><span class="p">(())</span><span class="w"></span>
<span class="w">    </span><span class="p">})</span><span class="o">?</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="n">write</span><span class="o">!</span><span class="p">(</span><span class="n">f</span><span class="p">,</span><span class="w"> </span><span class="s">")"</span><span class="p">)</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="sd">/// Sets up the callbacks in prior crates which we want to refer to the</span>
<span class="sd">/// TyCtxt in.</span>
<span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">setup_callbacks</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">rustc_span</span>::<span class="n">SPAN_DEBUG</span><span class="p">.</span><span class="n">swap</span><span class="p">(</span><span class="o">&amp;</span><span class="p">(</span><span class="n">span_debug</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="k">fn</span><span class="p">(</span><span class="n">_</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="n">fmt</span>::<span class="n">Formatter</span><span class="o">&lt;'</span><span class="nb">_</span><span class="o">&gt;</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">_</span><span class="p">));</span><span class="w"></span>
<span class="w">    </span><span class="n">rustc_hir</span>::<span class="n">def_id</span>::<span class="n">DEF_ID_DEBUG</span><span class="p">.</span><span class="n">swap</span><span class="p">(</span><span class="o">&amp;</span><span class="p">(</span><span class="n">def_id_debug</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="k">fn</span><span class="p">(</span><span class="n">_</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="n">fmt</span>::<span class="n">Formatter</span><span class="o">&lt;'</span><span class="nb">_</span><span class="o">&gt;</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">_</span><span class="p">));</span><span class="w"></span>
<span class="w">    </span><span class="n">TRACK_DIAGNOSTICS</span><span class="p">.</span><span class="n">swap</span><span class="p">(</span><span class="o">&amp;</span><span class="p">(</span><span class="n">track_diagnostic</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="k">fn</span><span class="p">(</span><span class="o">&amp;</span><span class="n">_</span><span class="p">)));</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="214462550"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/get%20the%20%28maybe%29%20def%20path%20of%20an%20InstanceDef/near/214462550" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oliver <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/get.20the.20(maybe).20def.20path.20of.20an.20InstanceDef.html#214462550">(Oct 25 2020 at 02:13)</a>:</h4>
<p>(deleted)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>